from flask import Flask, render_template
import sqlite3
import jieba  # 分词，将句子划分为单词
from matplotlib import pyplot as plt  # 绘图，数据可视乎
from past.builtins import reload
from wordcloud import WordCloud  # 词云
from PIL import Image  # 图片处理
import numpy as np  # 矩阵运算

app = Flask(__name__)


@app.route('/')
def index():
    con = sqlite3.connect("./database/giteeSpider.db")
    cur = con.cursor()
    sql = "select count(*) from projectList"
    data = cur.execute(sql)
    num = 0
    for i in data:
        num = i[0]
    cur.close()
    con.close()
    con = sqlite3.connect("./database/giteeSpider.db")
    cur = con.cursor()
    languageNum = 0
    data2 = cur.execute("select count(DISTINCT(mainLanguage)) from projectList")
    for i in data2:
        languageNum = i[0]
    cur.close()
    con.close()
    print("共使用", languageNum, "种语言")
    print("共有", num, "个项目")
    return render_template("index.html", num=num, languageNum=languageNum)


@app.route('/getWordCloud')
def getWordCloud():
    text = ""
    con = sqlite3.connect("./database/giteeSpider.db")
    cur = con.cursor()
    sql = "select mainLanguage from projectList;"
    data = cur.execute(sql)
    for items in data:
        for j in range(len(items)):
            text += items[j].replace("3个月前","").replace("10个月前","")
            text += " "
    cur.close()
    con.close()
    text.replace("\n","")
    print(text,"text")
    cut = jieba.cut(text)
    string = ' '.join(cut)
    print(len(string))

    my_dpi = 1000

    img = Image.open("./static/green.png")
    img_array = np.array(img)
    wc = WordCloud(
        background_color='white',
        mask=img_array,
        font_path="./static/font2.ttf",
    )
    wc.generate_from_text(string)
    # 开始绘制图片
    fig = plt.figure(1)
    plt.imshow(wc)
    plt.axis('off')
    # 输出词云到文件
    plt.savefig('static/wordCloud.jpg', dpi=my_dpi)
    plt.show()

    return "生成图片"


@app.route('/index')
def home():
    return index()


@app.route('/projects')
def project():
    datalist = []
    con = sqlite3.connect("./database/giteeSpider.db")
    cur = con.cursor()
    sql = "select * from projectList"
    list = cur.execute(sql)
    num = 0
    for i in list:
        datalist.append(i)
    cur.close()
    con.close()

    print(len(datalist))
    return render_template("projects.html", datalist=datalist)


@app.route('/score')
def score():
    top100 = []
    con = sqlite3.connect("./database/giteeSpider.db")
    cur = con.cursor()
    sql = "select * from projectList order by starNum desc limit 100 ;"
    list = cur.execute(sql)
    num = 0
    for i in list:
        item = []
        for a in i:
            item.append(a)
        top100.append(item)

    cur.close()
    con.close()

    # print(top100)
    namelist = []
    starlist = []
    gvpnum = 0
    supportnum = 0
    for i in top100:
        pass
        # print(i)
        namelist.append(str(i[1]))
        starlist.append(i[7])
        if str(i[3]) == "True":
            supportnum +=1
        if str(i[4]) == "True":
            gvpnum +=1
    print(namelist)
    print(starlist)
    print(gvpnum,supportnum)
    analy = [{"value":100,"name":"普通项目数量"},{"value":gvpnum,"name":"获得giteeGVP推荐"},{"value":supportnum,"name":"获得gitee推荐项目称号"}]
    return render_template("score.html",top100=top100,namelist=namelist,starlist=starlist,analy=analy)


@app.route('/word')
def word():
    return render_template("word.html")


@app.route('/team')
def team():
    return render_template("team.html")


if __name__ == '__main__':
    app.run()
